package com.oneonefive.educationsystem.controller;

import com.oneonefive.educationsystem.domain.Semester;
import com.oneonefive.educationsystem.dto.Result;
import com.oneonefive.educationsystem.service.SemesterService;
import com.oneonefive.educationsystem.utils.Constant;
import com.oneonefive.educationsystem.vo.SemesterVo;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RestController
@CrossOrigin
@RequestMapping("/semester")
public class SemesterController {
    @Resource
    SemesterService semesterService;

    @PostMapping()
    @PreAuthorize("hasAuthority('admin')")
    public Result semesterSave(@RequestBody Semester semester) {
        Result legitimacy = semesterService.legitimacy(semester);
        if (!legitimacy.getSuccess()) {
            return legitimacy;
        }
        boolean success = semesterService.save(semester);
        if (success) {
            return Result.ok("semester新增成功");
        }
        return Result.fail("semester新增失败");
    }

    @DeleteMapping("/{semesterId}")
    @PreAuthorize("hasAuthority('admin')")
    public Result semesterDelete(@PathVariable Integer semesterId) {
        boolean success = semesterService.removeById(semesterId);
        if (success) {
            if (Objects.equals(Constant.SEMESTER.getSemesterId(), semesterId)) {
                Constant.SEMESTER = null;
            }
            return Result.ok("semester删除成功");

        }
        return Result.fail("semester删除失败");
    }

    @PutMapping()
    @PreAuthorize("hasAuthority('admin')")
    public Result semesterUpdate(@RequestBody Semester semester) {
        Result legitimacy = semesterService.legitimacy(semester);
        if (!legitimacy.getSuccess()) {
            return legitimacy;
        }
        boolean success = semesterService.updateById(semester);
        if (success) {
            return Result.ok("semester修改成功");
        }
        return Result.fail("semester修改失败");
    }

    //data:List<Semester>
    @PostMapping("/1")
    public Result semesterList(@RequestBody SemesterVo semesterVo) {
        return semesterService.semesterList(semesterVo);
    }

    //设置当前学期
    @PostMapping("/setCurTerm")
    @PreAuthorize("hasAuthority('admin')")
    public Result setCurrentSemester(@RequestBody Semester semester) {
        if (Constant.SEMESTER != null) {
            Constant.SEMESTER.setState(0);
            boolean success = semesterService.updateById(Constant.SEMESTER);
            if (!success) {
                return Result.fail("设置原本当前学期的状态不是当前学期失败");
            }
        }
        semester.setState(1);
        Constant.SEMESTER = semester;
        boolean success = semesterService.updateById(semester);
        if (success) {
            return Result.ok("设置当前学期成功");
        }
        return Result.fail("设置当前学期失败");
    }

    @DeleteMapping("/allDelete")
    @PreAuthorize("hasAuthority('admin')")
    public Result allDelete(@RequestBody List<Integer> list) {
        for (Integer integer : list) {
            boolean b = semesterService.removeById(integer);
            if (Objects.equals(Constant.SEMESTER.getSemesterId(), integer)) {
                Constant.SEMESTER = null;
                if (!b) {
                    return Result.fail(integer + "删除失败");
                }
            }
        }
        return Result.ok("批量删除成功");
    }
}
